﻿using System;
using System.Windows.Forms;
using System.Globalization;
using ComponentFactory.Krypton.Toolkit;

namespace OrionBanque.Forms
{
    public partial class Connexion : ComponentFactory.Krypton.Toolkit.KryptonForm
    {
        public Classe.Utilisateur uA;
        public bool cont = false;
        private CultureInfo EnglishCulture = new CultureInfo("en-US");
        private CultureInfo FrenchCulture = new CultureInfo("fr-FR");
        private System.Resources.ResourceManager RM = null;

        public Connexion()
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = FrenchCulture;
            InitializeComponent();
            DateTime d = DateTime.Now;
            this.kryptonGroupBox1.Values.Description = d.ToString(System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat); //DateTime.Now.Day.ToString("00") + "/" + DateTime.Now.Month.ToString("00") + "/" + DateTime.Now.Year;
        }

        private void OK_Click(object sender, EventArgs e)
        {
            if (ValideForm())
            {
                try
                {
                    Classe.Utilisateur u = Classe.Utilisateur.Charge(txtLogin.Text.Trim());
                    if (!u.Id.Equals(0))
                    {
                        if (!u.Mdp.Equals(txtMdp.Text.Trim()))
                            KryptonMessageBox.Show("Mauvais utilisateur et/ou mot de passe", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            //MessageBox.Show(RM.GetString("BadUserPwd"), "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        else
                        {
                            uA = u;
                            cont = true;
                            this.Close();
                        }
                    }
                    else
                    {
                        KryptonMessageBox.Show("Mauvais utilisateur et/ou mot de passe", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        //MessageBox.Show(RM.GetString("BadUserPwd"), "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
                catch (Exception ex)
                {
                    KryptonMessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private bool ValideForm()
        {
            bool retour = false;
            if (txtLogin.Text.Trim() != string.Empty && txtMdp.Text.Trim() != string.Empty)
            {
                retour = true;
            }
            else
                KryptonMessageBox.Show("Tous les champs sont requis", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                //MessageBox.Show(RM.GetString("EmptyField"), "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            return retour;
        }

        private void Connection_Load(object sender, EventArgs e)
        {
            // LeResourceManager prend en paramètre : nom_du_namespace.nom_de_la_ressource_principale
            RM = new System.Resources.ResourceManager("OrionBanque.Forms.Connection", System.Reflection.Assembly.GetExecutingAssembly());

        }

        private void buttonSpecAny2_Click(object sender, EventArgs e)
        {
            Forms.Utilisateur ua = new Forms.Utilisateur();
            ua.ShowDialog();
        }
    }
}
